Skip to content

Keep mapped-call temporaries local to global initializers - #665

Merged
nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-5y457wpss6
Nov 19, 2026
Merged

nunoplopes merged 1 commit into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-5y457wpss6

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

Global initializers containing mapped calls such as std::min(0, 0) fail to compile in refcount output because temporary operands become ordinary Rust statics but use thread-local access. Unsafe output handles this literal-only call, but also fails nonconstant temporary initialization, including mixed-type conversions.

cpp2rust/converter/converter.cpp now gives mapped-call arguments initializer-local storage through the enclosing full expression, including Rust-only cast temporaries. Enclosing calls share that storage for temporary arrays and records; hoisted bindings have unique names.

Refcount global assignments bind right-hand sides before entering the thread-local closure, keeping temporary initialization outside it and preserving C++17's right-operand-first sequencing for same-type assignments.

tests/unit/global_call_temporaries.cpp covers reference-taking consumers, mixed-type initialization, enclosing aggregates and global assignments. Both Rust expectations were added.

Still broken:

  • mixed-type compound-assignment sequencing, such as i += (i = 10, std::max(1.5, 0.5)), which still reads the left operand first;
  • constructor arguments and reference-member aggregates in global initializers, such as S t(std::max(3, 4) + 1) and R rr{std::min(5, 6) + 0};
  • refcount rule closures in global initializers, such as s.substr(std::min(1, 2), std::max(2, 3));
  • unsafe lifetime-extended static owners, such as const int &r = read(std::min(4, 5)); with a value-returning read;
  • std::max over std::string temporaries, which needs nontrivial result copying;
  • pre-existing refcount dangling pointers for local read(std::min(0, 1)), return read(std::max(2, 3)), and the same pattern in a mem-initializer.
Testing

Linux Release ninja check: lit on tests/unit, tests/std, tests/ub and tests/multi-file reports 645 passed, 33 expected failures, no unexpected failures and no skips, compared with 644 passed and the same 33 expected failures on the base. Cargo reports 78 libcc2rs unit tests passed, 1 libcc2rs-macros compile-fail test passed and 22 control-flow tests passed, with no failures or ignored tests.

The original fixture, before adding the assignment cases, fails on the base with 58 refcount rustc errors (E0015, E0277, E0599) and 24 unsafe errors (E0015, E0133). The expanded fixture compiles and matches native execution in both models. clang-format 23 reports no changes for the changed C++ files.

To ask for a change, comment or review in plain words.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
@nunoplopes
nunoplopes merged commit 09eee7b into Cpp2Rust:master Nov 19, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants